package com.novellotus.iov.carvice.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.activity.AddOutGoingActivity;
import com.novellotus.iov.carvice.activity.AddStorageCarActivity;
import com.novellotus.iov.carvice.bean.RxStorageCar;
import com.novellotus.iov.carvice.bean.RxVehicle;
import com.novellotus.iov.carvice.interfaces.AdapterBottomInterface;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.utils.MyUtils;
import com.novellotus.iov.carvice.utils.MyViewUtils;
import com.novellotus.iov.carvice.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Rich on 2017/12/27.
 */

public class StorageSearchAdapter extends RecyclerView.Adapter implements AdapterBottomInterface {

    private final int BOTTOM = 100;
    private boolean showBottom = false;
    private int type;//1是入库 2是出库
    private List<RxVehicle.RecordsBean> recordsBeanList;
    private String eCode;
    private String rCode;
    private boolean add;
    private Context context;

    //  List<RxStorageCar> rxStorageCarList;
    public StorageSearchAdapter(Context context, String eCode, String rCode, List<RxVehicle.RecordsBean> recordsBeanList, int type) {
        this.context = context;
        this.eCode = eCode;
        this.rCode = rCode;
        this.type = type;
        if (recordsBeanList == null) {
            this.recordsBeanList = new ArrayList<>();
        } else {
            this.recordsBeanList = recordsBeanList;
        }
    }

    /**
     * 是否是新增状态
     */
    public void changeStatue(boolean add) {
        this.add = add;
        notifyDataSetChanged();
    }

//    public void setReportList(List<RxStorageCar> rxStorageCarList){
//        notifyDataSetChanged();
//    }

//    private boolean isReport(String vin){
//        if(rxStorageCarList==null)
//            return false;
//        for (RxStorageCar rxStorageCar:rxStorageCarList) {
//            if(rxStorageCar!=null){
//                if(vin.equals(rxStorageCar.getVin()))
//                    return true;
//            }
//        }
//        return  false;
//
//    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == BOTTOM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_bottom, parent, false);
            return new BottomHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_storage_car_item, null);
            return new CarHolder(view);
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (position == recordsBeanList.size()) {
            return BOTTOM;
        } else {
            return 1;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (recordsBeanList.size() == position) {
            initBottom((BottomHolder) holder, position);
        } else {
            initCar((CarHolder) holder, position);
        }
    }

    @Override
    public int getItemCount() {
        if (showBottom) {
            return recordsBeanList.size() + 1;
        } else {
            return recordsBeanList.size();
        }

    }

    private void initBottom(BottomHolder holder, int position) {
        if (add) {
            holder.tv_bottom.setTextColor(Color.parseColor("#ffffff"));
            holder.view_left.setBackgroundColor(Color.parseColor("#ffffff"));
            holder.view_right.setBackgroundColor(Color.parseColor("#ffffff"));

        } else {
            holder.tv_bottom.setTextColor(Color.parseColor("#333333"));
            holder.view_left.setBackgroundColor(Color.parseColor("#a1a1a1"));
            holder.view_right.setBackgroundColor(Color.parseColor("#a1a1a1"));

        }


    }

    //入库=1 出售=2
    private void initCar(CarHolder carHolder, final int position) {
        if (recordsBeanList.get(position) != null) {


            carHolder.tv_name.setText(recordsBeanList.get(position).getNickName());
            carHolder.tv_vin.setText(recordsBeanList.get(position).getVin());
            carHolder.tv_model.setText(recordsBeanList.get(position).getName());
            carHolder.tv_num.setText(recordsBeanList.get(position).getCode());
            carHolder.tv_time.setText(TimeUtils.StampToDate(recordsBeanList.get(position).getCreateTime() + ""));
            MyViewUtils.initImage(recordsBeanList.get(position).getLogo(), carHolder.img_logo, context);

            if (recordsBeanList.get(position).getStatus().equals("2")) {
                carHolder.tv_statue.setText("出库");
                carHolder.img_statue.setBackgroundResource(R.mipmap.icon_output);
                carHolder.tv_report.setText("");
                carHolder.tv_plate.setVisibility(View.VISIBLE);
                carHolder.tv_plate.setText(MyUtils.getPlateString(recordsBeanList.get(position).getPn()));
            } else {
                carHolder.tv_plate.setVisibility(View.GONE);
                carHolder.tv_statue.setText("入库");
                carHolder.img_statue.setBackgroundResource(R.mipmap.icon_storage);
                if (recordsBeanList.get(position).isReport()) {
                    carHolder.tv_report.setText("数据已上报");
                    carHolder.tv_report.setTextColor(Color.parseColor("#2eb9fc"));
                } else {
                    carHolder.tv_report.setTextColor(Color.parseColor("#a1a1a1"));
                    carHolder.tv_report.setText("数据未上报");
                }
            }


            carHolder.car.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (type == 1) {
                        if (recordsBeanList.get(position).getStatus().equals("1")) {
                            Intent intent = new Intent(context, AddStorageCarActivity.class);
                            intent.putExtra("eCode", eCode);
                            intent.putExtra("rCode", rCode);
                            intent.putExtra("carId", recordsBeanList.get(position).getId());
                            context.startActivity(intent);
                        } else {
                            Intent intent = new Intent(context, AddOutGoingActivity.class);
                            intent.putExtra("eCode", eCode);
                            intent.putExtra("rCode", rCode);
                            intent.putExtra("carId", recordsBeanList.get(position).getId());
                            context.startActivity(intent);

                        }

                    } else {
                        //出厂列表
                        if (recordsBeanList.get(position).getStatus().equals("1")) {

                            if (recordsBeanList.get(position).isReport()) {
                                if (add) {
                                    Intent intent = new Intent(context, AddOutGoingActivity.class);
                                    intent.putExtra("eCode", eCode);
                                    intent.putExtra("rCode", rCode);
                                    intent.putExtra("vin", recordsBeanList.get(position).getVin());
                                    intent.putExtra("typeId", recordsBeanList.get(position).getTypeId());
                                    intent.putExtra("code", recordsBeanList.get(position).getCode());
                                    intent.putExtra("name", recordsBeanList.get(position).getName());
                                    context.startActivity(intent);

                                } else {
                                    Intent intent = new Intent(context, AddStorageCarActivity.class);
                                    intent.putExtra("eCode", eCode);
                                    intent.putExtra("rCode", rCode);
                                    intent.putExtra("carId", recordsBeanList.get(position).getId());
                                    intent.putExtra("look", "hi");
                                    context.startActivity(intent);
                                }

                            } else {
                                Intent intent = new Intent(context, AddStorageCarActivity.class);
                                intent.putExtra("eCode", eCode);
                                intent.putExtra("rCode", rCode);
                                intent.putExtra("carId", recordsBeanList.get(position).getId());
                                intent.putExtra("look", "hi");
                                context.startActivity(intent);
                            }

                        } else {
                            Intent intent = new Intent(context, AddOutGoingActivity.class);
                            intent.putExtra("eCode", eCode);
                            intent.putExtra("rCode", rCode);
                            intent.putExtra("carId", recordsBeanList.get(position).getId());
                            context.startActivity(intent);
                        }

                    }


                }
            });
        }

    }


    public void updateList(List<RxVehicle.RecordsBean> recordsBeanList) {
        this.recordsBeanList = recordsBeanList;
        showBottom = recordsBeanList.size() >= 4;
        notifyDataSetChanged();
    }

    public void addList(List<RxVehicle.RecordsBean> recordsBeanList) {
        this.recordsBeanList.addAll(recordsBeanList);
        showBottom = recordsBeanList.size() >= 4;
        notifyDataSetChanged();
    }

    class CarHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_vin)
        TextView tv_vin;
        @BindView(R.id.tv_model)
        TextView tv_model;
        @BindView(R.id.tv_num)
        TextView tv_num;
        @BindView(R.id.tv_time)
        TextView tv_time;
        @BindView(R.id.tv_statue)
        TextView tv_statue;
        @BindView(R.id.img_statue)
        ImageView img_statue;
        @BindView(R.id.car)
        CardView car;
        @BindView(R.id.img_logo)
        ImageView img_logo;
        @BindView(R.id.tv_report)
        TextView tv_report;
        @BindView(R.id.tv_plate)
        TextView tv_plate;

        CarHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class BottomHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.rlt_bottom)
        RelativeLayout rlt_bottom;
        @BindView(R.id.tv_bottom)
        TextView tv_bottom;
        @BindView(R.id.view_left)
        View view_left;
        @BindView(R.id.view_right)
        View view_right;

        BottomHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    public void showBottomLayout() {
        showBottom = recordsBeanList.size() >= 4;
        notifyDataSetChanged();
    }

    @Override
    public void hideBottomLayout() {
        showBottom = false;
        notifyDataSetChanged();
    }
}
